package com.atguigu.gmall.common.service;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: zm
 * @Date: 2021/8/25 21:48
 */
@Service
public class RabbitService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    //   封装发送普通消息的方法！
    public boolean sendMessage(String exchange, String routingKey, Object message) {
        //  发送消息方法！
        rabbitTemplate.convertAndSend(exchange, routingKey, message);
        return true;
    }


    //   封装发送延时消息的方法！
    public boolean sendDelayMessage(String exchange, String routingKey, Object message, Integer delayTime) {
        //  发送消息方法！
        rabbitTemplate.convertAndSend(exchange, routingKey, message, message1 -> {
            message1.getMessageProperties().setDelay(delayTime * 1000); // 设置队列的过期时间
//            message1.getMessageProperties().setExpiration(delayTime.toString()); // 设置单独信息的过期时间
            return message1;
        });
        return true;
    }


}
